//
//  DYWBaseNavController.swift
//  sinaWeibo
//
//  Created by 董艳武 on 16/2/24.
//  Copyright © 2016年 董艳武. All rights reserved.
//

import UIKit

class DYWBaseNavController: UINavigationController,UIGestureRecognizerDelegate {
    
    // 一旦自定义了返回按钮，原有的返回手势就会失效
    override func viewDidLoad() {
        super.viewDidLoad()
        // 设置 pop 手势的代理方法
        self.interactivePopGestureRecognizer?.delegate = self
    }
    
    override func pushViewController(viewController: UIViewController, animated: Bool) {
        
        // 不是根视图控制器的时候显示左侧的导航栏按钮
        if self.childViewControllers.count != 0 {
            
            let btn = UIButton(img: "navigationbar_back_withtext", bgImg: nil , title: "返回", titleFontSize: 14, titleColor: UIColor.blackColor())
            // 隐藏 tabBar
            viewController.hidesBottomBarWhenPushed = true
            // 调用这个方法的控制器的左侧导航栏按钮
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn)
            btn.addTarget(self, action: "didClickToGoBack", forControlEvents: .TouchUpInside)
        }
        super.pushViewController(viewController, animated: animated)
    }
    
    @objc private func didClickToGoBack () {
        print("baseNav")
        self.popViewControllerAnimated(true)
    }
    
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        return self.childViewControllers.count > 1
    }
    
    
}
